package com.bookshop.book.data;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.bookshop.book.logic.Book;
import com.bookshop.dataOperator.DatabaseUtils;

public class SearchAssist {
	public Book byID(String matchword) {
		ResultSet rs = DatabaseUtils.getResultSet("book", "Id", matchword);
		try {
			rs.last();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		try {
			String date = rs.getString("publishDate");
			Date publishDate;
			if (date.equals("")) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				publishDate = sdf.parse(rs.getString("publishDate"));
			} else {
				publishDate = new Date();
			}

			double a = Double.parseDouble(rs.getString(8));
			int b = Integer.parseInt(rs.getString(10));
			byte[] image = Assistant.imageDecode(rs.getString(11));
			Book book = new Book(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), publishDate, a,
					rs.getString(9), b, image);
			return book;
		} catch (SQLException e) {
			e.printStackTrace();

		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;

	}

	public Book[] searchlist(String sql) {
		ResultSet rs = DatabaseUtils.pdSearch(sql);
		try {
			rs.last();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Book[] book = null;
		try {
			book = new Book[rs.getRow()];
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		try {
			rs.first();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		try {
			for (int i = 0; i < book.length; i++) {
				byte[] image = Assistant.imageDecode(rs.getString(11));
				try {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					Date publishDate = sdf.parse(rs.getString("publishDate"));
					double a = Double.parseDouble(rs.getString(8));
					int b = Integer.parseInt(rs.getString(10));
					book[i] = new Book(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), publishDate, a,
							rs.getString(9), b, image);
					rs.next();
				} catch (SQLException e) {
					e.printStackTrace();
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return book;

	}

	public Book[] byKeyword(String keyword, String matchword) {
		ResultSet rs = DatabaseUtils.getResultSet("book", keyword, matchword);
		try {
			rs.last();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		Book[] book = null;
		try {
			book = new Book[rs.getRow()];
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		try {
			rs.first();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		int i = 0;
		try {
			do {
				byte[] image = Assistant.imageDecode(rs.getString(11));
				try {
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					Date publishDate = sdf.parse(rs.getString("publishDate"));
					double a = Double.parseDouble(rs.getString(8));
					int b = Integer.parseInt(rs.getString(10));
					book[i++] = new Book(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), publishDate, a,
							rs.getString(9), b, image);
				} catch (SQLException e) {
					e.printStackTrace();
				} catch (ParseException e) {
					e.printStackTrace();
				}
			} while (rs.next());
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return book;

	}
}
